/*
 * @Description: 按钮权限
 * @Author: chenju
 * @Date: 2021-11-12 09:36:03
 * @LastEditors: chenju
 * @LastEditTime: 2021-11-12 16:45:07
 */
import type { Directive, App } from 'vue';
import { store } from '@/store';

const btnPermissionDirective: Directive = {
  beforeMount(el, binding) {
    // console.log('btnPermissionDirective beforeMount', store.state.user.permissions);
    const permissions: any[] = store.state.user.permissions || [];
    const permissionCode = binding.value;
    // console.log('btnPermissionDirective beforeMount permissionCode', permissionCode);
    if (permissions.includes(permissionCode)) {
      el.style.display = null;
    } else {
      el.style.display = 'none';
    }
  }
};

export function setupBtnPermissionDirective(app: App): void {
  app.directive('btnPermission', btnPermissionDirective);
}

export function hasBtnPermission(type: string) {
  return store.state.user.permissions?.includes(type);
}
